﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient; // 引入MySQL的命名空间

namespace parking_program
{
    public partial class login_form : Form
    {
        public login_form()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void register_link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            register_form register_Form = new register_form();
            register_Form.Show();
            this.Hide();
        }

        private void reset_link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            reset_form reset_Form = new reset_form();
            reset_Form.Show();
            this.Hide();
        }

        private void login_button_Click(object sender, EventArgs e)
        {
            string userid = id_textbox.Text;
            string password = password_textbox.Text; // 使用密码输入框的文本

            // 从配置文件中读取数据库连接信息
            MySqlConfigReaderClass configReader = new MySqlConfigReaderClass("D:\\Cshell-out\\parking\\config.txt");
            MySqlBaseInfoClass mySqlBaseInfo = configReader.MySqlBaseInfo;

            // 创建MySQL查询类实例
            MySqlQueryClass mySqlQuery = new MySqlQueryClass(mySqlBaseInfo.ServerIp, mySqlBaseInfo.DatabaseUsername, mySqlBaseInfo.DatabasePassword, mySqlBaseInfo.DatabaseName);

            // 构建查询语句，这里假设有一个名为 'User' 的表，其中包含 'Username' 和 'Password' 列
            string query = "SELECT COUNT(1) FROM User WHERE User_id = @Userid AND Password = @Password";
            MySqlParameter[] parameters = new MySqlParameter[]
            {
                new MySqlParameter("@Userid", userid),
                new MySqlParameter("@Password", password)
            };

            // 执行查询
            DataTable result = mySqlQuery.QueryData(query, parameters);

            // 检查查询结果
            if (result.Rows.Count > 0 && Convert.ToInt32(result.Rows[0][0]) == 1)
            {
                // 登录成功
                MessageBox.Show("登录成功！");
                // 这里可以进行后续的登录成功操作，比如隐藏登录窗口，显示主窗口等
                user_in user_In = new user_in();
                user_In.Show();
                this.Hide();
            }
            else
            {
                // 登录失败
                MessageBox.Show("登录失败，用户名或密码错误。");
            }
        }
    }
}
